package com.stedu.mall.adminService.controller;

import cn.hutool.captcha.LineCaptcha;
import cn.hutool.core.util.IdUtil;
import com.stedu.mall.common.bean.Admin;
import com.stedu.mall.common.bean.RespBean;
import com.stedu.mall.common.exception.SteduException;
import com.stedu.mall.common.service.AdminService;
import com.stedu.mall.common.uitl.JwtUtil;
import com.stedu.mall.common.uitl.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private AdminService adminService;
    @GetMapping("/captcha")
    public RespBean captcha(){
        //创建验证码对象
        LineCaptcha captcha = new LineCaptcha(120,38,4,10);
        //生成唯一的key
        String key = IdUtil.getSnowflakeNextIdStr();
        //将key和验证码文本保存到Redis中
        redisUtil.set(key,captcha.getCode(),60);
        //将验证码图片转换成Base64编码
        String imageBase64Data = captcha.getImageBase64Data();
        //将key和Base64编码保存在map中
        HashMap<String,Object> map = new HashMap<>();
        map.put("key",key);
        map.put("imageBase64Data", imageBase64Data);
        //将key和Base64图片返回
        return RespBean.ok("生成验证码成功",map);
    }

    @PostMapping("/login")
    public RespBean login(String username,String password,String key,String captchaInput) throws SteduException {
        //判断验证码是否正确 - 从Redis中查询验证码判断
        String captcha = (String)redisUtil.get(key);
        if(captcha == null || !captcha.equalsIgnoreCase(captchaInput)){
            throw new SteduException("验证码错误，请确认后重新输入");
        }
        //登录
        Admin admin = adminService.login(username, password);
        //生成token
        HashMap<String,Object>map = new HashMap<>();
        map.put("id", admin.getId());
        map.put("username", admin.getUsername());
        map.put("role", "admin");
        String jwtStr = JwtUtil.generateJwt(map);
        //将token返回给用户
        return RespBean.ok("登陆成功", jwtStr);
    }
    @GetMapping("/info")
    public RespBean getLogin(@RequestHeader("token") String token){
        Map<String,Object> map = JwtUtil.parseJwtToMap(token);
        Integer id = (Integer) map.get("id");
        Admin admin = adminService.selectById(id);
        //返回
        return  RespBean.ok("",admin);
    }
    //修改密码
    @PutMapping("/chgPwd")
    public RespBean caqPwd(String oldPwd,String newPwd,String newPwd1, @RequestHeader("token") String token) throws SteduException {
        if(!newPwd.equals(newPwd1)){
            throw new SteduException("两次输入的密码不一致");
        }
        Map<String,Object> map = JwtUtil.parseJwtToMap(token);
        Integer id = (Integer)map.get("id");
        adminService.chgPwd(oldPwd, newPwd, id);
        return RespBean.ok("修改成功,请重新登录");
    }
    @PutMapping("chgInfo")
    public RespBean chgInfo(@RequestBody Admin admin){
        admin.setUsername(null);
        adminService.update(admin);
        return RespBean.ok("修改个人信息成功");
    }

    @GetMapping("/selectAll")
    public RespBean selectAll(Integer status) {
        List<Admin> admins = adminService.selectAll(status);
        return RespBean.ok("", admins);
    }

    @DeleteMapping("/{id}")
    public RespBean delete(@PathVariable("id") Integer id) {
        if (adminService.selectById(id).getStatus() == 0) {
            adminService.delete(id);
            return RespBean.ok("禁用成功");
        } else {
            adminService.delete(id);
            return RespBean.ok("启用成功");
        }

    }

    @PostMapping("/insert")
    public RespBean inset(@RequestBody Admin admin) throws SteduException {
        adminService.insert(admin);
        return RespBean.ok("添加成功");
    }

    @PutMapping("/reset/{id}")
    public RespBean reset(@PathVariable("id") Integer id) {
        Admin admin = adminService.selectById(id);
        adminService.update(admin);
        return RespBean.ok("修改成功");
    }
}
